### 
#Usage: Rscript cancer_boxplot.R -jt cancer_boxplot.csv -log TRUE
#       Rscript cancer_boxplot.R -jt cancer_boxplot.csv -log FALSE
###


library(getopt)
library(ggplot2)
#library(reshape2)
#library(tidyr)
library(ggpubr)
spec <- matrix(
  c("junction_table",  "jt", 2, "character", "This is cpm table!",
    "log_scale", "log", 2, "character",  "whether log!"),
  byrow=TRUE, ncol=5)
args=getopt(spec)
data=read.csv(args$junction_table,header=F)
colnames(data)=c("cpm","class","cancer")
data$class = gsub('cancer', 'T', data$class)
data$class = gsub('NT', 'N', data$class)
data$cancer=as.factor(data$cancer)
data$class=as.factor(data$class)
print(head(data))
levels(data$class)=c("N(Normal)","T(Tumor)")

if (args$log=="TRUE"){
data$cpm=log2(data$cpm+1)
pdf(file="cancer_boxplot_log.pdf", width=14, height=6.5)
p = ggplot(data = data, aes(x=cancer,y=cpm, fill=factor(class))) +
  scale_fill_manual(values=c( "#00AFBB", "#F8766D"))+
  geom_boxplot(outlier.shape= NA) + 
  labs(fill = 'class') + 
  geom_point(position=position_jitterdodge(), size=1)+
  stat_compare_means(aes(group=class),label = "p.signif")+
  xlab("Cancer")+
  theme(axis.title = element_text(size = 10, face = "bold"))+
  ylab("log2(CPM+1)")+
  theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA))+
  theme(axis.text.x = element_text(size =8,angle = 45, hjust = 1, face="bold"))+
  theme(axis.text.y = element_text(size =8,angle = 360, hjust = 1, face="bold"))+
  theme(panel.background = element_rect(fill = "white", colour = "white"), panel.grid.major = element_blank())+
  theme(legend.position = 'top', legend.text = element_text(size =10, face="bold"))
print(p)
}else{
pdf(file="cancer_boxplot_noLog.pdf", width=14, height=6.5)
p = ggplot(data = data, aes(x=cancer,y=cpm, fill=factor(class))) +
  scale_fill_manual(values=c( "#00AFBB", "#F8766D"))+
  geom_boxplot(outlier.shape= NA) + 
  labs(fill = 'class') + 
  geom_point(position=position_jitterdodge(), size=1)+
  stat_compare_means(aes(group=class),label = "p.signif")+
  xlab("Cancer")+
  theme(axis.title = element_text(size = 10, face = "bold"))+
  ylab("CPM")+
  theme(panel.border = element_rect(linetype = 'solid', size = 1.2,fill = NA))+
  theme(axis.text.x = element_text(size =8,angle = 45, hjust = 1, face="bold"))+
  theme(axis.text.y = element_text(size =8,angle = 360, hjust = 1, face="bold"))+
  theme(panel.background = element_rect(fill = "white", colour = "white"), panel.grid.major = element_blank())+
  theme(legend.position = 'top', legend.text = element_text(size =10, face="bold"))
print(p)
}
dev.off()







